package com.renren.project.db.bean;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@AllArgsConstructor
@Data
@ApiModel(value = "分页查询结果")
public class PageResult<T> {

    @ApiModelProperty("总记录数")
    private long total;

    @ApiModelProperty("列表数据")
    private List<T> data;

    public static <T> PageResult<T> result(Page<T> page) {
        return new PageResult<>(page.getTotal(), page.getRecords());
    }

    public static <T, R> PageResult<R> result(Page<T> page, Function<T, R> function) {
        List<R> list = page.getRecords().stream().map(function).collect(Collectors.toList());
        return new PageResult<>(page.getTotal(), list);
    }

    public static <T> PageResult<T> result(long total, List<T> list) {
        return new PageResult<>(total, list);
    }

    public static <T, R> PageResult<R> result(long total, List<T> list, Function<T, R> function) {
        return new PageResult<>(total, list.stream().map(function).collect(Collectors.toList()));
    }
}
